//获取节点数据
function getElementData(treeId, treeNode){
	 $.ajax({
   		type:"POST",
   		url:$(path).val()+"/config/viewElement.do",
   		data:{
   				elementId:treeNode.id
   			},
   		async: false,
   		cache: false,
   		success: function(data){
   			var jsonStr=eval("("+data+")");
   			var element=jsonStr.element;
   			var elementInfo=jsonStr.elementInfo;

            //$("#parentElement").val(element.parentelementid);
           // $("#parentElementId").val(element.parentelementid);
			if(treeNode.pId==0){$("#parentElement").val("无上级机构");$("#parentElementId").val("-1");}
			else{
				var node = treeNode.getParentNode();
				$("#parentElement").val(node.name);$("#parentElementId").val(treeNode.pId);
			}

   			$("#parentelementid").val(element.parentelementid);
				$("#elementid").val(element.elementid);
				$("#elementname").val(element.elementname);
				$("#elementtype").val(element.elementtype);
				$("#citycode").val(element.citycode);
				$("#cityName").val(element.cityName);
				$("#iseffect").val(element.iseffect);
				$("#elementstatus").val(element.elementstatus);
				$("#isextends").val(element.isextends);
				$("#elementmode").val(element.elementmode);
				$("#bars_domain").val(element.domain);
				$("#tradeCode").val(elementInfo.tradecode);
				$("#propertycode").val(elementInfo.propertycode);
				$("#address").val(elementInfo.address);
				$("#name").val(elementInfo.name);
				$("#phonenum").val(elementInfo.phonenum);
				$("#desc").val(elementInfo.desc);
				$("#statement").val(elementInfo.statement);
				if($("#elementmode").val()==1){
					$("#ifBarsDomain").css("display","");
				}else{
					$("#ifBarsDomain").css("display","none");
				}
				
   		},
   		error: function(data){
   			swal("系统异常,请联系系统管理员!", "", "error");
   		}
   	});
};

//=============================ztree setting======================================================		 
function addHoverDom(treeId, treeNode) {
     var sObj = $("#" + treeNode.tId + "_span");
     if (treeNode.editNameFlag || $("#viewBtn_"+treeNode.tId).length>0) return;
     
     //当前节点为机构，则添加增加按钮
     var addStr ="";
     if(treeNode.type !=2){
    	 addStr += "<span class='button add' id='addBtn_" + treeNode.tId + "' title='增加'></span>";
     }
     
     addStr += "<span class='button remove' id='removeBtn_" + treeNode.tId+ "' title='删除'></span>";  
     addStr += "<span class='button edit' id='editBtn_" + treeNode.tId + "' title='编辑'></span>";
     addStr += "<span class='button view' id='viewBtn_" + treeNode.tId + "' title='查看'></span>";
     
     sObj.after(addStr);
     var addBtn = $("#addBtn_"+treeNode.tId);
     var removeBtn = $("#removeBtn_"+treeNode.tId);
     var editBtn = $("#editBtn_"+treeNode.tId);
     var viewBtn = $("#viewBtn_"+treeNode.tId);
//======================================= +号添加=======================================
     if (addBtn) addBtn.bind("click", function(){
        //var zTree = $.fn.zTree.getZTreeObj("treeDemo");
       //  zTree.addNodes(treeNode, {id:(1000 + newCount), pId:treeNode.id, name:"new node" + (newCount++)});
        // return false;
        // alert(treeId+"***"+treeNode.tId+"***"+treeNode.id+"***"+treeNode.pId);
         $("#elementtype").removeAttr("disabled");
    	 $("#parentelementid").val(treeNode.id);
		 $("#parentElement").val(treeNode.name);
		 $("#parentElementId").val(treeNode.id);
		 $("#elementmode").val(treeNode.mode);
		 $("#parentElement").attr("disabled","disabled");//上级节点     不可改变
		 $("#elementmode").attr("disabled","disabled");// 组网模式
		 $("#citycode").val(treeNode.cityCode);  //地市默认和上级一致
		 $("#cityName").val(treeNode.cityName);
		 $("#cityName").attr("disabled","disabled");
		 $("#elementtype").val(2);						//节点类型
		 $("#isextends").removeAttr("disabled");        //  配置类型
		 if($("#elementmode").val() == 1){             //  判断组网模式  --》银行是否显示
			 $("#ifBarsDomain").css("display","");     //  显示
		 }else {
             $("#ifBarsDomain").css("display", "none");//不显示
         }

		 $("#elementstatus_hide").css("display", "none");   //  节点状态 不显示
         //===============================================tree=====================
         model_tree();
         //=============================================================================

         $('#myModal').modal({
             show:true,
             backdrop:'static'
         });
         //关闭sideMenu
         $(".toggler").trigger("click");
    	 $('#myModal').modal({
         	show:true,
         	backdrop:'static'
         });
         return false;
     });
	//======================================删除=========================================
     if (removeBtn) removeBtn.bind("click", function(){
    		 if(treeNode.isParent){
    		    swal({
    		        title: "确定要删除节点 \"" + treeNode.name + "\" 吗？",
    		        text: "不会删除子节点！",
    		        type: "warning",
    		        showCancelButton: true,
    		        confirmButtonColor: "#DD6B55",
    		        confirmButtonText: "删除",
    		        cancelButtonText: "取消",
    		        closeOnConfirm: false
    		    }, function () {
    		    	$.ajax({
    		    		type:"POST",
    		    		url:$(path).val()+"/config/delElement.do",
    		    		data:{
    		    				elementId:treeNode.id
    		    			},
    		    		async: false,
    		    		cache: false,
    		    		success: function(data){
    		    			swal("删除成功！", "", "success");
    		    			var ztree = $("#organization_structure_ztree");
    		    			showTree(ztree);
    		    		},
    		    		error: function(data){
    		    			swal("系统异常,请联系系统管理员!", "", "error");
    		    		}
    		    	});
    		    });
    		 }else{
    			 swal({
     		        title: "",
     		        text: "确定要删除节点 \"" + treeNode.name + "\" 吗？",
     		        type: "warning",
     		        showCancelButton: true,
     		        confirmButtonColor: "#DD6B55",
     		        confirmButtonText: "删除",
     		        cancelButtonText: "取消",
     		        closeOnConfirm: false
     		    }, function () {
     		    	$.ajax({
    		    		type:"POST",
    		    		url:$(path).val()+"/config/delElement.do",
    		    		data:{
    		    				elementId:treeNode.id
    		    			},
    		    		async: false,
    		    		cache: false,
    		    		success: function(data){
    		    			swal("删除成功！", "", "success");
    		    			var ztree = $("#organization_structure_ztree");
    		    			showTree(ztree);
    		    		},
    		    		error: function(data){
    		    			swal("系统异常,请联系系统管理员!", "", "error");
    		    		}
    		    	});
     		    });
    		 }
    		
    		 return false;
      });
	//=======================================编辑========================================
     if (editBtn) editBtn.bind("click", function(){
    	$("#command").val(2);
		 $("#element_mode").val(treeNode.mode);
    	$("#elementId").val(treeNode.id);
         $("#addElementMode").val(1);
         //$("#isextends").val(1);
         if(treeNode.pId!=0){
             $("#isextends").removeAttr("disabled");
         }
         //$("#isextends").removeAttr("disabled");
		 if(treeNode.type==2)//  网点类型   显示状态
			 $("#elementstatus_hide").css("display","");
		 else{
			 $("#elementstatus_hide").css("display","none");
		 }
         //===============================================tree=====================
         model_tree();
         //=============================================================================
    	//地市不可修改
		 $("#citycode").val(treeNode.cityCode);
		 $("#cityName").val(treeNode.cityName);
		 $("#citycode").data("default",treeNode.cityCode);
		 $("#cityName").data("default",treeNode.cityName);
		 $("#cityName").attr("disabled","disabled");
		 //组网模式
		 $("#elementmode").attr("disabled","disabled");
         // 上级机构

         //$("#ifparentDomain").css("display","");
    	getElementData(treeId, treeNode);
    	$('#myModal').modal({
         	show:true,
         	backdrop:'static'
         });
         return false;
     });
	//=======================================查看========================================
     if (viewBtn) viewBtn.bind("click", function(){
    	//获取节点数据
    	getElementData(treeId, treeNode);
    	//构造地市树
		var ztree = $("#modal_ztree_city");
		showCityTree(ztree);
    	$('#Btn_saveElement').hide();
		 // 上级机构
		 if(treeNode.type==2)//  网点类型   显示状态
		 $("#elementstatus_hide").css("display","");
		 else{
			 $("#elementstatus_hide").css("display","none");
		 }
		// $("#ifparentDomain").css("display","");
    	//查看详情操作下的对话框，所有控件都不可用。
    	$("#myModal .modal-body :input").each(function () { 
    		$(this).attr("disabled", true);
    	}); 

     	$('#myModal').modal({
         	show:true,
         	backdrop:'static'
         });
         return false;
     });
 };

function removeHoverDom(treeId, treeNode) {
     $("#addBtn_"+treeNode.tId).unbind().remove();
     $("#removeBtn_"+treeNode.tId).unbind().remove();
     $("#editBtn_"+treeNode.tId).unbind().remove();
     $("#viewBtn_"+treeNode.tId).unbind().remove();
	  
 };

//=====================添加机构和网都是的组织结构树setting===================================
      var structure_setting = {
	        check: {
	            enable: false,
	        },
	        view: {
	            addHoverDom: addHoverDom,
            	removeHoverDom: removeHoverDom,
	            dblClickExpand: false,
	            showLine: true,
	            selectedMulti: false,
	            txtSelectedEnable:true
	        },
	        data: {
	            simpleData: {
	                enable:true,
	                idKey: "id",
	                pIdKey: "pId",
	                rootPId: "0"
	            }
	        },
	        callback: {
	            //onCheck:onCheck,
	            onClick:onClick1,
	        }
	    };
//=====================搜索区域组织结构树setting============================================
   var setting = {
	        check: {
	            enable: false,
	        },
	        view: {
	            dblClickExpand: false,
	            showLine: true,
	            selectedMulti: false,
	            txtSelectedEnable:true
	        },
	        data: {
	            simpleData: {
	                enable:true,
	                idKey: "id",
	                pIdKey: "pId",
	                rootPId: "0"
	            }
	        },
	        callback: {
	            //onCheck:onCheck,
	            onClick:onClick2,
	        }
	    };
//=====================modal中组织结构树setting============================================
   var modal_setting = {
	        check: {
	            enable: false,
	        },
	        view: {
	            dblClickExpand: false,
	            showLine: true,
	            selectedMulti: false,
	            txtSelectedEnable:true
	        },
	        data: {
	            simpleData: {
	                enable:true,
	                idKey: "id",
	                pIdKey: "pId",
	                rootPId: "0"
	            }
	        },
	        callback: {
	            //onCheck:onCheck,
	            onClick:onClick3,
	        }
	    };
   //=====================aiwifi设备配置中组织结构树setting============================================
   var modal_setting_aiwifi = {
	        check: {
	            enable: false,
	        },
	        view: {
	            dblClickExpand: false,
	            showLine: true,
	            selectedMulti: false,
	            txtSelectedEnable:true
	        },
	        data: {
	            simpleData: {
	                enable:true,
	                idKey: "id",
	                pIdKey: "pId",
	                rootPId: "0"
	            }
	        },
	        callback: {
	            onClick:onClickaiwifi,
	        }
	    };

//可以选择机构
var modal_setting2 = {
	check: {
		enable: false,
	},
	view: {
		dblClickExpand: false,
		showLine: true,
		selectedMulti: false,
		txtSelectedEnable:true
	},
	data: {
		simpleData: {
			enable:true,
			idKey: "id",
			pIdKey: "pId",
			rootPId: "0"
		}
	},
	callback: {
		onClick:onClick3_2,
	}
};
//=====================网点modal中组织结构树setting=========================================
   var modal_setting_element = {
	        check: {
	            enable: false,
	        },
	        view: {
	            dblClickExpand: false,
	            showLine: true,
	            selectedMulti: false,
	            txtSelectedEnable:true
	        },
	        data: {
	            simpleData: {
	                enable:true,
	                idKey: "id",
	                pIdKey: "pId",
	                rootPId: "0"
	            }
	        },
	        callback: {
	            //onCheck:onCheck,
	            onClick:onClick4,
	        }
	    };
//=====================组织结构 上级机构 结构树setting=======================================
var modal_parent_setting = {
    check: {
        enable: false,
    },
    view: {
        dblClickExpand: false,
        showLine: true,
        selectedMulti: false,
        txtSelectedEnable:true
    },
    data: {
        simpleData: {
            enable:true,
            idKey: "id",
            pIdKey: "pId",
            rootPId: "0"
        }
    },
    callback: {
        onClick:onClick5,
    }
};
//=======================根据选择的网点加载网关信息===========================================
		function updateGwDeviceId(elementId){
			
			$("#enterprise_Gateway\\.deviceId")
		    .html("<option value=''>加载设备ID中...</option>")
		    .attr("disabled", "disabled");
			$.ajax({
		        url: $(path).val()+"/monitor/getEnterprise_GatewayList.do",
		        type: "GET",
		        data:{elementId:elementId},
		        success: function (data) {
		            $("#enterprise_Gateway\\.deviceId").removeAttr("disabled");
		            var s;
					 if (data.length > 0 ) {
						 $(data).each(function(){
			                  s+= '<option value='+this.egId+'#'+this.deviceId+'>'+this.mibName+'</option>';
						 });
						
					  } 
					 $("#enterprise_Gateway\\.deviceId").empty();
					 $("#enterprise_Gateway\\.deviceId").html(s);
					 
		        }
		    });
			
		}
//=======================根据选择的网点加载aiwifi设备信息===========================================
		function updateAiWIFIDeviceId(elementId){
			$("#devName")
		    .html("<option value=''>加载设备ID中...</option>")
		    .attr("disabled", "disabled");
			$.ajax({
		        url: $(path).val()+"/monitor/getAiWIFIListByelId.do",
		        type: "POST",
		        data:{elementId:elementId,type:$("#type").val()},
		        success: function (data) {
		            $("#devName") .removeAttr("disabled");
		            var s;
					 if (data.length > 0 ) {
						 $(data).each(function(){
		                  s+= '<option value='+this.deviceId+'>'+this.devName+'</option>';
						 });

					  }
					 $("#devName").empty();
					 $("#devName").html(s);
		        }
		    });

		}
//------------------------------树节点对应的点击事件-------------------------------------
		function onClick1(e,treeId, treeNode){
		 //刷新节点属性
		 $('#elementid').val(treeNode.id);
		 freshAttribute();
	   }
		function onClick2(e,treeId, treeNode){
		   $("#netWorkId").val(treeNode.id);
		   $("#search").val(treeNode.name);
		   $("#treeDropDown").hide();
		   }
		function onClick3(e,treeId, treeNode){
	       if(treeNode.type==2){
               $("#modal_netWorkId").val(treeNode.id);
               $("#modal_search").val(treeNode.name);
               //设置所属网点的值
               $("#element\\.elementname").val(treeNode.name);
               $("#elId").val(treeNode.id);
               //关闭选择网点模态框
               $('#ElementModal').modal('hide');
               //根据选择的网点加载网关信息
               updateGwDeviceId($("#elId").val());
           }
           else{
			   $("#modal_search").val("");
			   $("#modal_search").attr("placeholder","不能选择机构");
		   }
		   }
		
			function onClickaiwifi(e,treeId, treeNode){
		       if(treeNode.type==2){
	               $("#modal_netWorkId").val(treeNode.id);
	               $("#modal_search").val(treeNode.name);
	               //设置所属网点的值
	               $("#elementName").val(treeNode.name);
	               $("#elId").val(treeNode.id);
	               //关闭选择网点模态框
	               $('#ElementModal').modal('hide');
	               //根据选择的网点加载网关信息
	               updateAiWIFIDeviceId($("#elId").val());
	           }
	           else{
				   $("#modal_search").val("");
				   $("#modal_search").attr("placeholder","不能选择机构");
			   }
			   }
		
		
function onClick3_2(e,treeId, treeNode){

		$("#modal_netWorkId").val(treeNode.id);
		$("#modal_search").val(treeNode.name);
		//设置所属网点的值
		$("#element\\.elementname").val(treeNode.name);
		$("#elId").val(treeNode.id);
		//关闭选择网点模态框
		$('#ElementModal').modal('hide');
}


		function onClick4(e,treeId, treeNode){
		   $("#modal_netWorkId").val(treeNode.id);
		   $("#modal_search").val(treeNode.name);
		   //设置所属网点的值
			$("#elementName").val(treeNode.name);
			$("#elementid").val(treeNode.id);
		   //关闭选择网点模态框
		   $('#ElementModal').modal('hide');
		   }
		function onClick5(e,treeId, treeNode){
		   //设置所属网点的值
//addMode   添加按钮不同  0 : 添加机构按钮   1：每个机构后面的加号
			var addMode=$("#addElementMode").val();
			   if($("#elementid").val()==treeNode.id&&addMode==1){
				   $("#parentelementid").val("");
				   $("#parentElement").val("");
				   $("#parentElement").attr("placeholder","上级机构不能为本身");
			   }
			   else if(treeNode.id!=0&&addMode==1){
				   if($("#element_mode").val()!=treeNode.mode) {
					   $("#parentelementid").val("");
					   $("#parentElement").val("");
					   $("#parentElement").attr("placeholder", "组网模式不一致!");
				   }
				   else {
				   	//=======================有效点击后==============================
					   $("#parentelementid").val(treeNode.id);
					   $("#parentElement").val(treeNode.name);
					   $("#citycode").val(treeNode.cityCode);
					   $("#cityName").val(treeNode.cityName);
                       $("#isextends").val(1);
                       $("#isextends").removeAttr("disabled");
                       if(treeNode.id==0){
                           $("#isextends").val(2);
                           $("#isextends").attr("disabled","true");
                       }
                       if(addMode==0&&treeNode.id!=0){ //有父节点
						   //组网模式
                           $("#elementmode").val(treeNode.mode);
                           $("#elementmode").attr("disabled","disabled");
						   //地市
						   $("#cityName").attr("disabled","disabled");
                           if($("#elementmode").val() == 1){
                               $("#ifBarsDomain").css("display","");
                           }else{
                               $("#ifBarsDomain").css("display","none");
                           }
                       }
                       else if(addMode==0&&treeNode.id==0){ //无父节点
                           $("#elementmode").removeAttr("disabled");
						   $("#cityName").removeAttr("disabled");
                           $("#isextends").val(2);
                           $("#isextends").attr("disabled","true");
                       }
					   $("#treeDropDown2").hide();
				   }
			   }
			   else {
				   //=======================有效点击后==============================
				   $("#parentelementid").val(treeNode.id);
				   $("#parentElement").val(treeNode.name);
				   if(treeNode.id!=0) {
					   $("#citycode").val(treeNode.cityCode);
					   $("#cityName").val(treeNode.cityName);
				   }
				   else{
					   $("#citycode").val($("#citycode").data("default"));
					   $("#cityName").val($("#cityName").data("default"));
				   }
                   $("#isextends").val(1);
                   $("#isextends").removeAttr("disabled");
                   if(treeNode.id==0){
                       $("#isextends").val(2);
                       $("#isextends").attr("disabled","true");
                   }
                   if(addMode==0&&treeNode.id!=0){
					   //组网模式
					   $("#elementmode").val(treeNode.mode);
					   $("#elementmode").attr("disabled","disabled");
					   //地市
					   $("#cityName").attr("disabled","disabled");
                       if($("#elementmode").val() == 1){
                           $("#ifBarsDomain").css("display","");
                       }else{
                           $("#ifBarsDomain").css("display","none");
                       }
                   }
                   else if(addMode==0&&treeNode.id==0){
                       $("#elementmode").removeAttr("disabled");
					   $("#cityName").removeAttr("disabled");
                       $("#isextends").val(2);
                       $("#isextends").attr("disabled","true");
                   }
				   $("#treeDropDown2").hide();
			   }

		   }
	   
